#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
using namespace std;
int main()
{
    int pipefd[2];
    if(pipe(pipefd) == -1)
    {
        cerr << "pipe open fault" << endl;
        return 1;
    }
    pid_t pid = fork();
    if(pid < 0)
    {
        cerr << "fork fault" << endl;
        return 1;
    }
    else if(pid == 0)
    {
        //子进程
        close(pipefd[0]);//子进程关闭读端，子进程写
        char buffer[] = "hello linux";
        write(pipefd[1],buffer,11);
        exit(0);
    }
    
    //父进程
    close(pipefd[1]);//父进程关闭写端，父进程读
    close(pipefd[0]);//父进程关闭读端
    int status = 0; 
    waitpid(pid,&status,0);
    cout << "子进程是否正常退出:" << WIFEXITED(status) << endl;
    cout << "子进程的退出信号为：" << (status & 0x007f) << endl;
    return 0;
}